<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对数组排序</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.sizeof.html">sizeof</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.uasort.html">uasort</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.sort" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sort</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">sort</span> &mdash; <span class="dc-title">对数组排序</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.sort-description">
  <h3 class="title">说明</h3>
   <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>sort</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sort_flags</code><span class="initializer"> = SORT_REGULAR</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   本函数对数组进行排序。当本函数结束时数组单元将被从最低到最高重新安排。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.sort-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">array</code></em></dt>

     <dd>

      <p class="para">
       要排序的数组。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">sort_flags</code></em></dt>

     <dd>

      <p class="para">
       可选的第二个参数 <em><code class="parameter">sort_flags</code></em>
   可以用以下值改变排序的行为：
      </p>
      <p class="para">
       排序类型标记：
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_REGULAR</code></strong>  - 正常比较单元（不改变类型）</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_NUMERIC</code></strong> - 单元被作为数字来比较</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_STRING</code></strong> - 单元被作为字符串来比较</span>
        </li>
        <li class="listitem">
         <span class="simpara"> <strong><code>SORT_LOCALE_STRING</code></strong> -
      根据当前的区域（locale）设置来把单元当作字符串比较，可以用
      <span class="function"><a href="function.setlocale.html" class="function">setlocale()</a></span> 来改变。
          
         </span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_NATURAL</code></strong> - 和 <span class="function"><a href="function.natsort.html" class="function">natsort()</a></span> 类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_FLAG_CASE</code></strong> - 能够与 <strong><code>SORT_STRING</code></strong> 或 
         <strong><code>SORT_NATURAL</code></strong> 合并（OR 位运算），不区分大小写排序字符串。</span>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.sort-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.sort-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>5.4.0</td>
      <td>
       添加了 <em><code class="parameter">sort_flags</code></em> 内 <strong><code>SORT_NATURAL</code></strong> 和
       <strong><code>SORT_FLAG_CASE</code></strong> 的支持。 
      </td>
     </tr>

     <tr>
      <td>5.0.2</td>
      <td>
       添加了  <strong><code>SORT_LOCALE_STRING</code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="refsect1 examples" id="refsect1-function.sort-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4933">
    <p><strong>Example #1 <span class="function"><strong>sort()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"fruits["&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"]&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   fruits 被按照字母顺序排序。
  </p>
  <p class="para">
   <div class="example" id="example-4934">
    <p><strong>Example #2 使用不区分大小写自然排序的 <span class="function"><strong>sort()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Orange1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"orange2"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Orange3"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"orange20"<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_NATURAL&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">SORT_FLAG_CASE</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"fruits["&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"]&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
fruits[0] = Orange1
fruits[1] = orange2
fruits[2] = Orange3
fruits[3] = orange20
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   fruits 排序得像 <span class="function"><a href="function.natcasesort.html" class="function">natcasesort()</a></span> 的结果。
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.sort-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数为 <em><code class="parameter">array</code></em>
中的元素赋与新的键名。这将删除原有的键名，而不是仅仅将键名重新排序。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    和大多数 PHP 排序函数一样，<span class="function"><strong>sort()</strong></span> 
    使用了 <a href="http://en.wikipedia.org/wiki/Quicksort" class="link external">&raquo;&nbsp;Quicksort</a> 实现的。
   </span>
  </p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    在对含有混合类型值的数组排序时要小心，因为
    <span class="function"><strong>sort()</strong></span> 可能会产生不可预知的结果。
   </p>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.sort-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.asort.html" class="function" rel="rdfs-seeAlso">asort()</a> - 对数组进行排序并保持索引关系</span></li>
    <li class="member"><a href="array.sorting.html" class="link">数组排序函数对比</a></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.sizeof.html">sizeof</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.uasort.html">uasort</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
